home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / FINDSUPPORT.PY < prev    next >
Encoding:
Python Source  |  2000-07-26  |  12.7 KB  |  370 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__="""Find support"""
  65. __version__='$Revision: 1.16.20.3 $'[11:-2]
  66.  
  67.  
  68. import sys, os, string, time, Globals, ExtensionClass
  69. from DocumentTemplate.DT_Util import Eval, expr_globals
  70. from AccessControl.Permission import name_trans
  71. from Globals import HTMLFile
  72. from DocumentTemplate.DT_Util import InstanceDict, TemplateDict
  73. from DateTime import DateTime
  74. from string import find
  75. from AccessControl import getSecurityManager
  76.  
  77. class FindSupport(ExtensionClass.Base):
  78.     """Find support for Zope Folders"""
  79.  
  80.     manage_findFrame=HTMLFile('findFrame', globals())
  81.     manage_findForm=HTMLFile('findForm', globals(), management_view='Find')
  82.     manage_findAdv=HTMLFile('findAdv', globals(), management_view='Find',
  83.                             help_topic='Find_Advanced.stx',
  84.                             help_product='OFSP')
  85.     manage_findResult=HTMLFile('findResult', globals())
  86.  
  87.     __ac_permissions__=(
  88.         ('View management screens',
  89.          ('manage_findFrame', 'manage_findForm', 'manage_findAdv',
  90.           'manage_findResult')),
  91.         )
  92.     
  93.     manage_options=(
  94.         {'label':'Find', 'action':'manage_findFrame', 'target':'manage_main',
  95.          'help':('OFSP','Find.stx')},         
  96.         )
  97.  
  98.     def ZopeFind(self, obj, obj_ids=None, obj_metatypes=None,
  99.                  obj_searchterm=None, obj_expr=None,
  100.                  obj_mtime=None, obj_mspec=None,
  101.                  obj_permission=None, obj_roles=None,
  102.                  search_sub=0,
  103.                  REQUEST=None, result=None, pre=''):
  104.         """Zope Find interface"""
  105.  
  106.         if result is None:
  107.             result=[]
  108.  
  109.             if obj_metatypes and 'all' in obj_metatypes:
  110.                 obj_metatypes=None
  111.                 
  112.             if obj_mtime and type(obj_mtime)==type('s'):
  113.                 obj_mtime=DateTime(obj_mtime).timeTime()
  114.  
  115.             if obj_permission:
  116.                 obj_permission=p_name(obj_permission)
  117.  
  118.             if obj_roles and type(obj_roles) is type('s'):
  119.                 obj_roles=[obj_roles]
  120.                 
  121.             if obj_expr:
  122.                 # Setup expr machinations
  123.                 md=td()
  124.                 obj_expr=(Eval(obj_expr, expr_globals), md, md._push, md._pop)
  125.  
  126.         base=obj
  127.         if hasattr(obj, 'aq_base'):
  128.             base=obj.aq_base
  129.  
  130.         if not hasattr(base, 'objectItems'):
  131.             return result
  132.         try:    items=obj.objectItems()
  133.         except: return result
  134.  
  135.         try: add_result=result.append
  136.         except:
  137.             raise AttributeError, `result`
  138.  
  139.         for id, ob in items:
  140.             if pre: p="%s/%s" % (pre, id)
  141.             else:   p=id
  142.             
  143.             dflag=0
  144.             if hasattr(ob, '_p_changed') and (ob._p_changed == None):
  145.                 dflag=1
  146.  
  147.             if hasattr(ob, 'aq_base'):
  148.                 bs=ob.aq_base
  149.             else: bs=ob
  150.  
  151.             if (
  152.                 (not obj_ids or absattr(bs.id) in obj_ids)
  153.                 and
  154.                 (not obj_metatypes or (hasattr(bs, 'meta_type') and
  155.                  bs.meta_type in obj_metatypes))
  156.                 and
  157.                 (not obj_searchterm or
  158.                  (hasattr(ob, 'PrincipiaSearchSource') and
  159.                   find(ob.PrincipiaSearchSource(), obj_searchterm) >= 0
  160.                   ))
  161.                 and
  162.                 (not obj_expr or expr_match(ob, obj_expr))
  163.                 and
  164.                 (not obj_mtime or mtime_match(ob, obj_mtime, obj_mspec))
  165.                 and
  166.                 ( (not obj_permission or not obj_roles) or \
  167.                    role_match(ob, obj_permission, obj_roles)
  168.                 )
  169.                 ):
  170.                 add_result((p, ob))
  171.                 dflag=0
  172.                     
  173.             if search_sub and hasattr(bs, 'objectItems'):
  174.                 self.ZopeFind(ob, obj_ids, obj_metatypes,
  175.                                    obj_searchterm, obj_expr,
  176.                                    obj_mtime, obj_mspec,
  177.                                    obj_permission, obj_roles,
  178.                                    search_sub,
  179.                                    REQUEST, result, p)
  180.             if dflag: ob._p_deactivate()
  181.  
  182.         return result
  183.  
  184.  
  185.  
  186.     
  187.     PrincipiaFind=ZopeFind
  188.  
  189.     def ZopeFindAndApply(self, obj, obj_ids=None, obj_metatypes=None,
  190.                          obj_searchterm=None, obj_expr=None,
  191.                          obj_mtime=None, obj_mspec=None,
  192.                          obj_permission=None, obj_roles=None,
  193.                          search_sub=0,
  194.                          REQUEST=None, result=None, pre='',
  195.                          apply_func=None, apply_path=''):
  196.         """Zope Find interface and apply"""
  197.  
  198.         if result is None:
  199.             result=[]
  200.  
  201.             if obj_metatypes and 'all' in obj_metatypes:
  202.                 obj_metatypes=None
  203.                 
  204.             if obj_mtime and type(obj_mtime)==type('s'):
  205.                 obj_mtime=DateTime(obj_mtime).timeTime()
  206.  
  207.             if obj_permission:
  208.                 obj_permission=p_name(obj_permission)
  209.  
  210.             if obj_roles and type(obj_roles) is type('s'):
  211.                 obj_roles=[obj_roles]
  212.                 
  213.             if obj_expr:
  214.                 # Setup expr machinations
  215.                 md=td()
  216.                 obj_expr=(Eval(obj_expr, expr_globals), md, md._push, md._pop)
  217.  
  218.         base=obj
  219.         if hasattr(obj, 'aq_base'):
  220.             base=obj.aq_base
  221.  
  222.         if not hasattr(base, 'objectItems'):
  223.             return result
  224.         try:    items=obj.objectItems()
  225.         except: return result
  226.  
  227.         try: add_result=result.append
  228.         except:
  229.             raise AttributeError, `result`
  230.  
  231.         for id, ob in items:
  232.             if pre: p="%s/%s" % (pre, id)
  233.             else:   p=id
  234.             
  235.             dflag=0
  236.             if hasattr(ob, '_p_changed') and (ob._p_changed == None):
  237.                 dflag=1
  238.  
  239.             if hasattr(ob, 'aq_base'):
  240.                 bs=ob.aq_base
  241.             else: bs=ob
  242.  
  243.             if (
  244.                 (not obj_ids or absattr(bs.id) in obj_ids)
  245.                 and
  246.                 (not obj_metatypes or (hasattr(bs, 'meta_type') and
  247.                  bs.meta_type in obj_metatypes))
  248.                 and
  249.                 (not obj_searchterm or
  250.                  (hasattr(ob, 'PrincipiaSearchSource') and
  251.                   find(ob.PrincipiaSearchSource(), obj_searchterm) >= 0
  252.                   ))
  253.                 and
  254.                 (not obj_expr or expr_match(ob, obj_expr))
  255.                 and
  256.                 (not obj_mtime or mtime_match(ob, obj_mtime, obj_mspec))
  257.                 and
  258.                 ( (not obj_permission or not obj_roles) or \
  259.                    role_match(ob, obj_permission, obj_roles)
  260.                 )
  261.                 ):
  262.                 if apply_func:
  263.                     apply_func(ob, (apply_path+'/'+p))
  264.                 else:
  265.                     add_result((p, ob))
  266.                     dflag=0
  267.                     
  268.             if search_sub and hasattr(bs, 'objectItems'):
  269.                 self.ZopeFindAndApply(ob, obj_ids, obj_metatypes,
  270.                                       obj_searchterm, obj_expr,
  271.                                       obj_mtime, obj_mspec,
  272.                                       obj_permission, obj_roles,
  273.                                       search_sub,
  274.                                       REQUEST, result, p,
  275.                                       apply_func, apply_path)
  276.             if dflag: ob._p_deactivate()
  277.  
  278.         return result
  279.  
  280.  
  281.  
  282.  
  283. class td(TemplateDict):
  284.  
  285.     def validate(self, inst, parent, name, value, md):
  286.         return getSecurityManager().validate(inst, parent, name, value)
  287.  
  288.  
  289. def expr_match(ob, ed, c=InstanceDict, r=0):
  290.     e, md, push, pop=ed
  291.     push(c(ob, md))
  292.     try: r=e.eval(md)
  293.     finally:
  294.         pop()
  295.         return r
  296.  
  297.  
  298.  
  299. def mtime_match(ob, t, q, fn=hasattr):
  300.     if not fn(ob, '_p_mtime'):
  301.         return 0    
  302.     return q=='<' and (ob._p_mtime < t) or (ob._p_mtime > t)
  303.  
  304.  
  305. def role_match(ob, permission, roles, lt=type([]), tt=type(())):
  306.     pr=[]
  307.     fn=pr.append
  308.     
  309.     while 1:
  310.         if hasattr(ob, permission):
  311.             p=getattr(ob, permission)
  312.             if type(p) is lt:
  313.                 map(fn, p)
  314.                 if hasattr(ob, 'aq_parent'):
  315.                     ob=ob.aq_parent
  316.                     continue
  317.                 break
  318.             if type(p) is tt:
  319.                 map(fn, p)
  320.                 break
  321.             if p is None:
  322.                 map(fn, ('Manager', 'Anonymous'))
  323.                 break
  324.  
  325.         if hasattr(ob, 'aq_parent'):
  326.             ob=ob.aq_parent
  327.             continue
  328.         break
  329.  
  330.     for role in roles:
  331.         if not (role in pr):
  332.             return 0
  333.     return 1
  334.  
  335.  
  336. Globals.default__class_init__(FindSupport)
  337.  
  338. # Helper functions
  339.  
  340. def absattr(attr):
  341.     if callable(attr): return attr()
  342.     return attr
  343.  
  344.  
  345. def p_name(name):
  346.     return '_' + string.translate(name, name_trans) + '_Permission'
  347.  
  348.  
  349.